import { HTTP_REQUEST_URL, } from '@/config/app';
import store from '../store';
//图片上传方法
export function uploadFilePromise(file) {
	uni.showLoading({
		title: '图片上传中',
		mask: true
	});
	const token = store.state.app.token;
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: HTTP_REQUEST_URL + '/apps/v1/common/upload', // 仅为示例，非真实的接口地址
			filePath: file,
			header: {
				token: token
			},
			name: 'file',
			success: (res) => {
				let response = JSON.parse(res.data)
				if (response.code == 1) {
					setTimeout(() => {
						resolve(response.data)
						uni.hideLoading()
					}, 1000)
				} else {
					uni.showToast({
						title: response.msg,
						icon: 'none'
					})
					uni.hideLoading()
				}
			},
		});
	})
}
/**
 * @desc 拼接OSS图片地址  
 * @param {string} url - 图片的文件名，包括扩展名  
 * @returns {string} - 拼接后的OSS图片地址  
 */
export function OssUrl(url) {
	return HTTP_REQUEST_URL + url;
}

/**
 * @desc 全局提示函数
 * @param {string} title - 提示的内容
 * @param {string} icon - 图标名字
 * @param {number} endtime -提示的延迟时间，单位毫秒
 */

export function Toast(title, icon = 'none', endtime = 2000) {
	uni.showToast({
		title: title,
		icon: icon,
		duration: endtime
	});
}
/**
 * @desc 页面跳转函数
 * @param {object|string} opt - 提示的内容
 * @param {object|string} to_url - 页面跳转地址
 * 例:
 * this.$Tips('/pages/test/test'); 跳转不提示
 * this.$Tips({title:'提示'},'/pages/test/test'); 提示并跳转
 * this.$Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
 */
const TAB_SWITCH = 1; //一定时间后跳转至 table上
const NAVIGATE = 2; //一定时间后跳转至非 table上
const NAVIGATE_BACK = 3; //一定时间后返回上页面
const RELAUNCH = 4; //关闭所有页面跳转至非table上
const REDIRECT = 5; //关闭当前页面跳转至table上
export function Tips(opt, to_url) {
	if (typeof opt == 'string') {
		to_url = opt;
		opt = {};
	}
	// 参数解构和默认值  
	const {
		title = '', icon = 'none', endtime = 2000
	} = opt;
	// 显示提示  
	if (title) Toast(title, icon, endtime)
	// 处理跳转逻辑  
	const handleRedirect = (delay, navigationFunc, navigationArgs) => {
		setTimeout(() => {
			navigationFunc(navigationArgs);
		}, delay);
	};
	if (to_url !== undefined) {
		if (typeof to_url === 'object') {
			const {
				tab = NAVIGATE, url = ''
			} = to_url;

			switch (tab) {
				case TAB_SWITCH:
					handleRedirect(endtime, uni.switchTab, {
						url
					});
					break;
				case NAVIGATE:
					handleRedirect(endtime, uni.navigateTo, {
						url
					});
					break;
				case NAVIGATE_BACK:
					const delta = parseInt(url, 10) || 1;
					handleRedirect(endtime, uni.navigateBack, {
						delta
					});
					break;
				case RELAUNCH:
					handleRedirect(endtime, uni.reLaunch, {
						url
					});
					break;
				case REDIRECT:
					handleRedirect(endtime, uni.redirectTo, {
						url
					});
					break;
				default:
					console.warn('Unsupported tab type:', tab);
			}
		} else if (typeof to_url === 'function') {
			handleRedirect(endtime, to_url, null);
		} else {
			// 直接跳转，不延迟  
			handleRedirect(title ? endtime : 0, uni.navigateTo, {
				url: to_url
			});
		}
	}
}
/**
 * @desc 函数防抖 (只执行最后一次点击)
 * @param fn
 * @param delay
 */
export const Debounce = (fn, t) => {
	const delay = t || 500
	let timer
	return function() {
		const args = arguments
		if (timer) {
			clearTimeout(timer)
		}
		timer = setTimeout(() => {
			timer = null
			fn.apply(this, args)
		}, delay)
	}
}
/**
 * @desc 函数节流 每 几s 执行一次。
 * @param func 函数
 * @param wait 延迟执行毫秒数
 */
export const Throttle = (func, wait) => {
	let timeout;
	return function() {
		let context = this;
		let args = arguments;
		if (!timeout) {
			timeout = setTimeout(() => {
				timeout = null;
				func.apply(context, args)
			}, wait)
		}
	}
}

export const timestampToTime = (timestamp) => {
  var date = new Date(timestamp);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}